/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Carter
 */
public final class SceneMonitorProperties {

    /** The one instance */
    private static SceneMonitorProperties instance = null;

    /** The properties object. */
    private static Properties properties;

    /** File handle */
    private static File file;

    /**
     * Private constructor.
     */
    private SceneMonitorProperties() {

    }

    /**
     * Reads an existing ini file and initializes.
     */
    private static void load() {

	properties = new Properties();

	file = new File(Constants.PROPSFILE);

	if(file.exists()) {

	    try {
		FileInputStream lcl_f = new FileInputStream(file);
		properties.load(lcl_f);
	    }
	    catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }

    /**
     * Returns the properties instance.
     * 
     * @return SceneMonitorProperties
     */
    public static SceneMonitorProperties getProperties() {

	if(instance == null)
	    instance = new SceneMonitorProperties();

	if(properties == null)
	    load();

	return instance;
    }

    /**
     * @param section
     * @param key
     * @return
     */
    public String getString(String key) {

	String value = properties.getProperty(key);

	return (value != null) ? value : "";
    }

    /**
     * 
     * @param section
     * @param key
     * @return
     */
    public int getInt(String key) {

	String sVal = getString(key);
	int value = 0;

	try {
	    value = Integer.parseInt(sVal);
	}
	catch (NumberFormatException ex) {
	    value = 0;
	}

	return value;
    }

    /**
     * 
     * @param section
     * @param key
     * @return
     */
    public float getFloat(String key) {

	String sVal = getString(key);
	float value = 0.0f;

	try {
	    value = Float.parseFloat(sVal);
	}
	catch (NumberFormatException ex) {
	    value = 0.0f;
	}

	return value;
    }

    /**
     * 
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {

	String sVal = getString(key);

	return sVal.compareToIgnoreCase("true") == 0;
    }

    /**
     * 
     * @param key
     * @param value
     */
    public void put(String key, String value) {

	properties.put(key, value);

	save();
    }

    /**
     * 
     * @param key
     * @param value
     */
    public void put(String key, int value) {

	put(key, Integer.toString(value));
    }

    /**
     * 
     * @param key
     * @param value
     */
    public void put(String key, float value) {

	put(key, Float.toString(value));
    }

    /**
     * 
     * @param section
     * @param key
     * @param value
     */
    public void put(String key, Boolean value) {

	put(key, value.toString());
    }

    /**
	 * 
	 *
	 */
    private void save() {

	try {
	    // noel lynch 21-08-08 : changed for java 5 compatability
	    // FileWriter writer = new FileWriter(file.getPath());
	    FileOutputStream lcl_f = new FileOutputStream(file);
	    properties.store(lcl_f, "Scene Monitor Properties");
	    
	}
	catch (IOException e) {
	    e.printStackTrace();
	}
    }
}
